package com.bcurtu.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.WebUtils;

import com.bcurtu.db.Account;


public class SignonInterceptor extends HandlerInterceptorAdapter {

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		Account userAccount = (Account) WebUtils.getSessionAttribute(request, "userAccount");
		if (userAccount == null) {
			String url = request.getServletPath();
			ModelAndView modelAndView = new ModelAndView("login");
			modelAndView.addObject("loginForwardAction", url);
			throw new ModelAndViewDefiningException(modelAndView);
		}
		else {
			return true;
		}
	}

}
